package com.genesis.mall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.genesis.mall.common.Result;
import com.genesis.mall.entity.UmsMember;
import com.genesis.mall.mapper.UmsMemberMapper;
import com.genesis.mall.service.IUmsMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.genesis.mall.utils.TokenUtil;
import org.springframework.security.crypto.bcrypt.BCrypt;
//import org.mindrot.jbcrypt.BCrypt;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author author
 * @since 2022-08-01
 */
@Service
public class UmsMemberServiceImpl extends ServiceImpl<UmsMemberMapper, UmsMember> implements IUmsMemberService {

    @Override
    public Result login(String username, String password) {

        LambdaQueryWrapper<UmsMember> lqw = new LambdaQueryWrapper<>();
        lqw.eq(UmsMember::getUsername,username);
        UmsMember umsMember = getOne(lqw);
        if (umsMember==null){
            return Result.error("用户不存在");
        }
        if (umsMember.getStatus()==0){
            return Result.error("账户已被禁用");
        }
        //进行密码校验
        boolean flag = BCrypt.checkpw(password, umsMember.getPassword());
        if (flag){//密码正确 将用户id存入token里
            HashMap<String, String> tokenMap = new HashMap<>();
            tokenMap.put("umsMember",umsMember.getId().toString());
            String token = TokenUtil.getToken(tokenMap);

            HashMap<String, String> map = new HashMap<>();
            map.put("tokenHead","Bearer");
            map.put("token",token);

            return Result.success(map);
        }
        return Result.error("密码错误");
    }
}
